package com.woniuxy.purchasemanager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.commons.entity.purchaseManager.PurchaseOrderEntity;
import com.woniuxy.commons.entity.purchaseManager.base.PageResult;
import com.woniuxy.commons.entity.purchaseManager.base.QueryPageBean;
import com.woniuxy.commons.entity.purchaseManager.dto.PurchaseOrderGoodsEditDTO;
import com.woniuxy.commons.result.Result;
import com.woniuxy.commons.utils.PageUtils;
import com.woniuxy.purchasemanager.service.PurchaseOrderGoodsService;
import com.woniuxy.purchasemanager.service.PurchaseOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 采购单表
 *
 * @author huangzhichao
 * @email 1205560871@gmail.com
 * @date 2022-04-19 13:00:28
 */
@Api(tags = "采购单表")
@RestController
@RequestMapping("/need/purchaseorder")
public class PurchaseOrderController {

    public static final Logger LOGGER = LoggerFactory.getLogger(PurchaseOrderController.class);

    @Autowired
    private PurchaseOrderService purchaseOrderService;

    @Resource
    private PurchaseOrderGoodsService purchaseOrderGoodsService;

    /**
     * 列表
     */
    @ApiOperation(value = "列表")
    @PostMapping("/list")
    public Result<PageUtils> list(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseOrderService.queryPage(params);

        return Result.ok(page);
    }

    /**
     * 分页查询+搜索查询+时间查询
     *
     * @param queryPageBean:
     * @Author: xianhao
     * @Date: 2022/4/24 19:14
     * @return: com.woniuxy.commons.entity.purchaseManager.base.PageResult<com.woniuxy.commons.entity.purchaseManager.PurchaseOrderEntity>
     **/
    @ApiOperation(value = "分页查询")
    @PostMapping("/findPage")
    public Result<PageResult<PurchaseOrderEntity>> findPage(@RequestBody QueryPageBean queryPageBean) {
        LOGGER.info("queryPageBean的信息是{}", queryPageBean);
        return Result.ok(purchaseOrderService.findPage(queryPageBean));
    }


    /**
     * 采购单导出列表
     *
     * @Author: xianhao
     * @Date: 2022/4/22 15:56
     * @return: com.woniuxy.commons.result.Result<java.util.List < com.woniuxy.commons.entity.purchaseManager.PurchaseOrderEntity>>
     **/
    @ApiOperation(value = "采购单导出列表")
    @GetMapping("/all")
    public Result<List<PurchaseOrderEntity>> selectAll() {
        return Result.ok(purchaseOrderService.list(new QueryWrapper<>()));
    }


    /**
     * 信息
     */
    @ApiOperation(value = "信息")
    @GetMapping("/info/{purchaseOrderId}")
    public Result<PurchaseOrderEntity> info(@PathVariable("purchaseOrderId") Integer purchaseOrderId) {
        PurchaseOrderEntity purchaseOrder = purchaseOrderService.getById(purchaseOrderId);

        return Result.ok(purchaseOrder);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存")
    @PostMapping("/save")
    public Result<PageUtils> save(@RequestBody PurchaseOrderEntity purchaseOrder) {
        purchaseOrderService.save(purchaseOrder);

        return Result.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public Result<PageUtils> update(@RequestBody PurchaseOrderEntity purchaseOrder) {
        purchaseOrderService.updateById(purchaseOrder);

        return Result.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/delete")
    public Result<PageUtils> delete(@RequestBody Integer[] purchaseOrderIds) {
        purchaseOrderService.removeByIds(Arrays.asList(purchaseOrderIds));

        return Result.ok();
    }

    /**
     * 获取采购单信息
     *
     * @param purchaseOrderId:
     * @Author: xianhao
     * @Date: 2022/4/25 11:17
     * @return: com.woniuxy.commons.result.Result<com.woniuxy.commons.entity.purchaseManager.dto.PurchaseOrderGoodsEditDTO>
     **/
    @ApiOperation(value = "获取采购单信息")
    @GetMapping("/getPurchaseOrderGoodsEditData/{purchaseOrderId}")
    public Result<PurchaseOrderEntity> getPurchaseOrder(@PathVariable("purchaseOrderId") Integer purchaseOrderId) {
        PurchaseOrderEntity byId = purchaseOrderService.getById(purchaseOrderId);
        return Result.ok(byId);
    }

    /**
     * 获取采购单对应商品信息
     * @Author: xianhao
     * @Date: 2022/4/28 16:36
     * @return: com.woniuxy.commons.result.Result<com.woniuxy.commons.entity.purchaseManager.dto.PurchaseOrderGoodsEditDTO>
     **/
    @ApiOperation(value = "获取采购单对应商品信息")
    @GetMapping("/getPurchaseOrderGoodsByOrderId/{purchaseOrderId}")
    public Result<PurchaseOrderGoodsEditDTO> getPurchaseOrderGoodsByOrderId(@PathVariable("purchaseOrderId") Integer purchaseOrderId){
        PurchaseOrderGoodsEditDTO purchaseOrderGoodsEditData = purchaseOrderService.getPurchaseOrderGoodsEditData(purchaseOrderId);
        return Result.ok(purchaseOrderGoodsEditData);
    }

    /**
     * 拆单业务
     *
     * @param splitGoodsIdsx:
     * @Author: xianhao
     * @Date: 2022/4/27
     * @return: com.woniuxy.commons.result.Result
     **/
    @ApiOperation(value = "采购单进行拆单处理")
    @PostMapping("/spit")
    public Result splitOrder(@RequestBody Map<String, Object> splitGoodsIdsx) {
        //拆单业务
        boolean flag = purchaseOrderService.split(splitGoodsIdsx);
        if (!flag) return Result.fail("拆单失败，请联系管理员！");
        return Result.ok("拆单成功");
    }

    /**
     * 审核
     *
     * @param purchaseOrderId:
     * @Author: xianhao
     * @Date: 2022/4/27 19:17
     * @return: com.woniuxy.commons.result.Result
     **/
    @GetMapping("/xMan/{purchaseOrderId}")
    public Result xMan(@PathVariable Integer purchaseOrderId) {
        boolean flag = purchaseOrderService.xMan(purchaseOrderId);
        if (!flag) return Result.fail("审核失败");
        return Result.ok("审核通过");
    }

    /**
     * 反审
     * @Author: xianhao
     * @Date: 2022/4/27 20:22
     * @param purchaseOrderId:
     * @return: com.woniuxy.commons.result.Result
     **/
    @GetMapping("/unxMan/{purchaseOrderId}")
    public Result unxMan(@PathVariable Integer purchaseOrderId) {
        boolean flag = purchaseOrderService.unxMan(purchaseOrderId);
        if (!flag) return Result.fail("反审失败");
        return Result.ok("反审通过");
    }

}
